using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoleController : MonoBehaviour
{
    // --------------------------------------------------------------------------------
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Vector3 moveDirection = Vector3.zero;
    public float rotateX;
    public Transform roleTransform;
    // --------------------------------------------------------------------------------
    // Start is called before the first frame update
    void Start()
    {
        roleTransform = transform.Find("Chan");
    }
    // --------------------------------------------------------------------------------
    // Update is called once per frame
    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = Vector3.zero;
            if (Input.GetKey(KeyCode.W))
            {
                var ts = GameObject.FindGameObjectWithTag("MainCamera").transform;
                moveDirection = ts.rotation * Vector3.forward;
                moveDirection.y = 0.0f;
                moveDirection *= speed;
                roleTransform.rotation = Quaternion.LookRotation(moveDirection);
            }
            else if (Input.GetKey(KeyCode.S))
            {
                var ts = GameObject.FindGameObjectWithTag("MainCamera").transform;
                moveDirection = ts.rotation * Vector3.back;
                moveDirection.y = 0.0f;
                moveDirection *= speed;
                roleTransform.rotation = Quaternion.LookRotation(moveDirection);
            }
            else if (Input.GetKey(KeyCode.A))
            {
                var ts = GameObject.FindGameObjectWithTag("MainCamera").transform;
                moveDirection = ts.rotation * Vector3.left;
                moveDirection.y = 0.0f;
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;
                roleTransform.rotation = Quaternion.LookRotation(moveDirection);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                var ts = GameObject.FindGameObjectWithTag("MainCamera").transform;
                moveDirection = ts.rotation * Vector3.right;
                moveDirection.y = 0.0f;
                moveDirection *= speed;
                roleTransform.rotation = Quaternion.LookRotation(moveDirection);
            }

            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);

    }

}
